package com.zdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdb.mapper.ArticleCategoryMapper;
import com.zdb.mapper.ArticleCollectingMapper;
import com.zdb.mapper.ArticleLikeMapper;
import com.zdb.mapper.ArticlesMapper;
import com.zdb.pojo.*;
import com.zdb.pojo.dto.ArticleDto;
import com.zdb.pojo.dto.ArticleSearchDto;
import com.zdb.pojo.vo.ArticleVO;
import com.zdb.service.ArticleCollectingService;
import com.zdb.service.ArticlesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service
public class ArticlesServiceImpl implements ArticlesService {
    private static final Logger log = LoggerFactory.getLogger(ArticlesServiceImpl.class);
    @Autowired
    private ArticlesMapper articlesMapper;

    @Autowired
    private ArticleLikeMapper articleLikeMapper;

    @Autowired
    private ArticleCollectingService articleCollectingService;

    @Autowired
    private ArticleCollectingMapper articleCollectingMapper;
    @Autowired
    private ArticleCategoryMapper articleCategoryMapper;
    @Override
    public ArticleVO getArticle(int id) {
        LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ArticleSearchDto build = ArticleSearchDto.builder().userId(principal.getId()).id(id).build();
      return    articlesMapper.getArticleVoById(build);

    }

    @Override
    public Integer deleteArticle(int id) {
        return articlesMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addArticle(ArticleDto article) {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        article.setUserId(user.getId());
         articlesMapper.insert(article);

        ArticleCategory build = ArticleCategory.builder().articleId(article.getId()).categoryId(article.getCategoryId()).build();
        articleCategoryMapper.insert(build);
        return article.getId();
    }

    @Override
    public Integer updateArticle(ArticleDto article) {
        return articlesMapper.updateById(article);
    }

    @Override
    public IPage<ArticleVO> getArticles(ArticleSearchDto articleSearchDto) {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        articleSearchDto.setUserId(user.getId());
        Page<ArticleVO> objectPage = new Page<>(articleSearchDto.getPage(), articleSearchDto.getLimit());
        IPage<ArticleVO> articles = articlesMapper.getArticles(objectPage, articleSearchDto);
        return articles;
    }

    @Override
    public void viewArticleIncrement(Integer id) {
        articlesMapper.viewArticleIncrement(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void likeArticleDecrement(Integer id) {

        LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        QueryWrapper<ArticleLike> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", id);
        wrapper.eq("user_id", principal.getId());
        if (articleLikeMapper.selectCount(wrapper) > 0){
            articleLikeMapper.delete(wrapper);
            articlesMapper.likeArticleDecrement(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void likeArticleIncrement(Integer id) {
        LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        QueryWrapper<ArticleLike> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", id);
        wrapper.eq("user_id", principal.getId());
        if(articleLikeMapper.selectCount(wrapper) > 0) {
            return;
        }
        ArticleLike likeBuilder = ArticleLike.builder().userId(principal.getId()).articleId(id).build();

        articleLikeMapper.insert(likeBuilder);
        articlesMapper.likeArticleIncrement(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void likeArticleHandleder(Integer id) {
        LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<ArticleLike> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", id);
        wrapper.eq("user_id", principal.getId());
        if (articleLikeMapper.selectCount(wrapper) > 0){
            articleLikeMapper.delete(wrapper);
            articlesMapper.likeArticleDecrement(id);
        } else {
            ArticleLike likeBuilder = ArticleLike.builder().userId(principal.getId()).articleId(id).build();
            articleLikeMapper.insert(likeBuilder);
            articlesMapper.likeArticleIncrement(id);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void collectingArticle(Integer id) {
        LoginUser principal = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<ArticleCollecting> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", id);
        wrapper.eq("user_id", principal.getId());
        if (articleCollectingMapper.selectCount( wrapper)>0){
            articleCollectingMapper.delete(wrapper);
//            articlesMapper.collectingArticleDecrement(id);
        }else {
            ArticleCollecting build = ArticleCollecting.builder().articleId(id).userId(principal.getId()).build();
            articleCollectingMapper.insert(build);
//            articlesMapper.collectingArticleincrement(id);
        }
    }
}
